package com.ruoyi.fly.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fly.domain.Notice;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
@Api(tags = "通知管理")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 查询通知列表
     *
     * @param noticeSearchVo
     * @return
     */
    @ApiOperation(value = "查询通知列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody NoticeSearchVo noticeSearchVo) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(noticeSearchVo,pageDomain);
        startPage(pageDomain);
        List<NoticeVo> list = noticeService.selectNoticeList(noticeSearchVo);
        return getDataTable(list);
    }

    /**
     * 根据id查询通知
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询通知")
    @GetMapping(value = "/selNoticeById/{id}")
    public R<Notice> getInfo(@PathVariable("id") String id) {
        if (id == null){
            return R.fail("id不能为空");
        }
        if (noticeService.selectNoticeById(id) == null) {
            return R.fail("通知不存在");
        }
        return R.ok(noticeService.selectNoticeById(id));
    }

    /**
     * 新增通知
     * @param notice
     * @return
     */
    @ApiOperation(value = "新增通知")
    @PostMapping("/addNotice")
    public R add(@RequestBody NoticeInsertVo notice) {
//        if (StringUtils.isNotBlank(notice.getNoticeTitle()) || StringUtils.isNotBlank(notice.getNoticeContent())
//                || StringUtils.isNotBlank(notice.getNoticeType())){
//            return R.fail("通知标题，内容，类型不能为空");
//        }
        return noticeService.insertNotice(notice);
    }

    /**
     * 修改通知
     * @param notice
     * @return
     */
    @ApiOperation(value = "修改通知")
    @PostMapping("/editNotice")
    public R edit(@RequestBody NoticeUpdateVo notice) {
        return noticeService.updateNotice(notice);
    }

    /**
     * 删除通知
     * @param deleteVo
     * @return
     */
    @ApiOperation(value = "删除通知")
    @PostMapping("/deleteNoticeById")
    public R remove(@RequestBody DeleteVo deleteVo) {
        if (StringUtils.isEmpty(deleteVo.getIds())) {
            return R.fail("参数不能为空");
        }
        return noticeService.deleteNoticeByIds(deleteVo.getIds());
    }
}
